let app = getApp();
let API_URL = app.globalData.API_URL;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIphone: app.globalData.isIphone,
    activeBox: 3,
    price: 5,
    commodity_type: 7,
    vipList: [],
    teChuanList: [
      'بارلىق مەزمۇنلاردىن ھەقسىز بەھىرلىنىسىز .',
      'باش سۈرەتكە ئەزالىق بەلگىسى قوشۇلۇپ كۆرۈنىدۇ .',
      ' ئەسەر يېڭىلىنىش ئەسكەرتىشى تاپشۇرۋالسىز .',
      'ئېلانسىز كۆرۈسىز ، ئاڭلايسىز .' 
    ],
    forbidMaskDialog: false,
    successDialog: false,
    isLoginMask: false,
  },
  // 获取价格
  getPrice() {
    let that = this;
    wx.request({
      url: `${app.globalData.API_URL}/targetPrices`,
      header: {
        'content-type': 'application/json'
      },
      success (res) {
        if(res.statusCode == 200) {
          that.setData({ vipList: res.data })
          for(let item of res.data) {
            if(item.id ==  that.data.activeBox) {
              that.setData({ price: item.price })
            }
          }
        }
      }
    });
    
  },

  // 购买会员
  confirmBuy() {
    if (app.globalData.isIphone) return this.setData({ forbidMaskDialog: true });
    
    // 支付
    let bodyCon = '周会员';
    switch (this.data.commodity_type) {
        case 1:
          bodyCon = '日会员';
          break;
        case 7:
          bodyCon = '周会员';
          break;
        case 30:
          bodyCon = '月会员';
          break;
        case 365:
          bodyCon = '年会员';
          break;
        default:
          break;
    }
    let commodity_type = this.data.commodity_type
    let price = this.data.price * 100
    let data = { bodyCon, commodity_type, price }
    let that = this;

    // 没登录的先登录再付款
    let promise = new Promise(function(resolve, rej) {
        app.login(res => {
          resolve(res)
        })
    })

    promise.then(
        resolve => {
            wx.request({
                url: `${API_URL}/payments`,
                data: data,
                header: app.header(),
                method: 'post',
                success: res=> {
                  wx.requestPayment({
                    nonceStr: res.data.nonceStr,
                    package: res.data.package,
                    paySign: res.data.paySign,
                    signType: "MD5",
                    timeStamp: res.data.timeStamp,
                    success(res) {
                      app.globalData.isVIPAuthority = true;
                      that.setData({ successDialog: true });
                    },
                    fail(res) {
                      wx.showToast({
                          title: "取消支付",
                          icon: 'fail',
                          duration: 2000,
                          mask: false
                      })
                    }
                  })
                }
            })
        }
    )
  },

  // 点击会员 Box
  onClickVipBox(item) {
    let price = item.currentTarget.dataset.item.price;
    let activeBoxId = item.currentTarget.dataset.item.id;
    let commodity_type = item.currentTarget.dataset.item.target_type
    this.setData({ activeBox: activeBoxId, price: price, commodity_type: commodity_type })
  },

  // 关闭苹果用户禁止付款对话框
  confirmForbidDialog() {
    this.setData({ forbidMaskDialog: false })
  },

  // 关闭付款成功对话框
  closeSuccessDialo(){
    this.setData({ successDialog: false })
    wx.navigateBack({
      delta: 1, 
    });
  },

  // 检查用户登录
  isLogin() {
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) {
      this.setData({ isLoginMask: true })
    }
  },

  // 隐藏登录提示组件
  changeLoginMask(e) {
    this.setData({ isLoginMask: e.detail.isLoginMask})
  },

  // 往联系页面
  goContactPage() {
    wx.navigateTo({
      url: '/pages/contact/contact',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPrice();
    this.isLogin();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: 'قىيات ئاۋازلىق ئەسەر',
      path: '/pages/member/member',
      imageUrl: 'https://mmecoa.qpic.cn/sz_mmecoa_png/iauAIibMdU78thJHXKBcIfYLNA6zH3pbFllMLlSibhVriaGwtS0bM1tLLyz6OSlQ74icF6c4Yic4icLokYGvb2jXnY8bA/640?wx_fmt=png&amp;from=appmsg'
    };
  }
})